/**
 * 字典类型模块
 */
layui.config({
  base: '/'
}).extend({
  flying: 'flying'
}).define(['index', 'flying'], function (exports) {  //先加载index模块，里面配置了全局的入口
  let $ = layui.$;
  let layer = layui.layer;
  let admin = layui.admin;
  let flying = layui.flying;
  let form = layui.form;
  let table = layui.table;
  let dropdown = layui.dropdown;

  flying.table({
    elem: '#LAY-dict-type-table',
    url: '/dict/type/getDictTypePageList', //数据接口
    cols: [
        // {type: 'checkbox', fixed: 'left'}
        {type: 'numbers', fixed: 'left', title: '序号'}
        , {field: 'dictDesc', title: '名称', minWidth: 100}
        , {field: 'dictType', title: '类型', minWidth: 100}
        , {field: 'status', title: '状态', minWidth: 90, templet: '#switchTpl', unresize: true}
        , {field: 'remark', title: '备注', minWidth: 80}
        , {field: 'updatedBy', title: '修改人', minWidth: 90}
        , {field: 'updatedTime', title: '修改时间', templet: '<div>{{ layui.util.toDateString(d.updatedTime, "yyyy-MM-dd HH:mm:ss") }}</div>', minWidth: 170}
        , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#dict-type-table'}
    ]
  });

  //监听工具条
  table.on('tool(LAY-dict-type-table)', function (obj) {
    let line_data = obj.data;
    if (obj.event === 'dict') {
      layer.open({
        type: 2
        , title: '字典枚举'
        , content: '/dict/code/list?dictType=' + line_data.dictType
        // ,maxmin: true
        , area: ['98.3%', '98.3%']
        , resize: false
        , btn: ['取消']
        , cancel: function (index, layero) {
          table.reload('LAY-dict-type-table'); //数据刷新
          layer.close(index); //关闭弹层
        }
      });
    } else if (obj.event === 'more') {
      //下拉菜单
      dropdown.render({
        elem: this, // 触发事件的 DOM 对象
        show: true, // 外部事件触发即显示
        align: 'left ' , // 右对齐弹出
        data: [
          {
            title: '修改',
            id: 'edit',
            templet: '<i class="layui-icon layui-icon-edit"></i> {{= d.title}}',
            href: '#'
          },
          {
            title: '删除',
            id: 'del',
            templet: '<i class="layui-icon layui-icon-delete"></i> {{= d.title }}',
            href: '#'
          }
        ],
        click: function (mdata) {
          if (mdata.id === "edit") {
            layer.open({
              type: 2
              , title: '修改'
              , content: '/dict/type/turn/edit?id=' + line_data.id
              // ,maxmin: true
              , area: ['500px', '540px']
              , resize: false
              , btn: ['确定', '取消']
              , yes: function (index, layero) {
                let iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-dict-type-front-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                  let field = data.field; //获取提交的字段
                  field.id = line_data.id;

                  //提交 Ajax 成功后，更新表格中的数据
                  admin.req({
                    url: '/dict/type/save'
                    , type: 'post'
                    , contentType: 'application/json;charset=utf-8'
                    , data: JSON.stringify(field)
                    , done: function (res) {
                      //执行成功
                      layer.msg(res.msg, {icon: 1});
                      table.reload('LAY-dict-type-table'); //数据刷新
                      layer.close(index); //关闭弹层
                    }
                  });
                });

                submit.trigger('click');
              }
              //打开弹层成功后的回调函数
              , success: function (layero, index) {

              }
            });
          } else if (mdata.id === "del") {
            layer.confirm('真的删除行么？', function (index) {
              //提交 Ajax 成功后，更新表格中的数据
              admin.req({
                url: '/dict/type/del?id=' + line_data.id
                , type: 'post'
                , done: function (res) {
                  //执行成功
                  layer.msg(res.msg, {icon: 1});
                  table.reload('LAY-dict-type-table'); //数据刷新
                  layer.close(index); //关闭弹层
                }
              });
            });
          }
        }
      });
    }
  });

  //对外暴露的接口
  exports('dictType', {});
});